package com.murphy.algorithm.likou;

import java.util.HashSet;
import java.util.Set;

/**
 * @author dongsufeng
 * @version 4.0
 * @date 2023/3/7 17:43
 */
public class HuanLinked {

    public ListNode validHuan(ListNode listNode){
        Set<ListNode> set = new HashSet<>();
        while (listNode != null){
           if (set.contains(listNode)){
               return listNode;
           }else {
               set.add(listNode);
               listNode = listNode.next;
           }
        }
        return null;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(3);
        l1.next = new ListNode(23);
        l1.next.next=new ListNode(9);
        l1.next.next.next=l1;
        HuanLinked huanLinked = new HuanLinked();
        ListNode listNode = huanLinked.validHuan(l1);
        if (listNode != null){
            System.out.println(listNode.val);
        }
    }

    public static class ListNode{
        private int val;
        private ListNode next;
        public ListNode(){}
        public ListNode(int val){
            this.val = val;
        }
    }
}
